package com.ruifu.doc.controller;

import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.common.vo.ResultVO;
import com.ruifu.doc.service.DmDocMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文档控制层
 *
 * @author nyr
 * @create 2019/9/9
 */
@RestController
@CrossOrigin
@RequestMapping("/dmDocMaster")
public class DmDocMasterController {

    @Autowired
    private DmDocMasterService dmDocMasterService;

    /**
     * 用于存储登陆人信息
     */
    private CloudUser cloudUser;

    /**
     * 通过url请求方法之前,调用该方法获取登陆人信息
     */
    @ModelAttribute
    public void before() {
        cloudUser = SecurityUtils.getUser();
    }

    /**
     * 根据文档代号查询是否存在
     *
     * @param dmDocCode 文档代号
     * @return
     */
    @GetMapping(value = "/findByDmDocCode/{dmDocCode}")
    public ResultVO findByDmDocCode(@PathVariable String dmDocCode) {
        if (dmDocMasterService.findByDmDocCode(dmDocCode, cloudUser)) {
            return new ResultVO(true, "文档代号查询不存在");
        } else {
            return new ResultVO(false, "文档代号查询已存在");
        }
    }

    /**
     * 获取文档编号
     * @param count
     * @return
     */
    @GetMapping(value = "/getDocCode")
    public List<String> getDocCode(@RequestParam(required = false,defaultValue = "1") Integer count) {
        return dmDocMasterService.getDocCode(count);
    }
}
